<script type="text/javascript" id="variables">    
    var almacenes = <?php echo $almacenes;?>;
    localStorage['almacenes'] = JSON.stringify(almacenes);    
</script>
<?php $javascript->link(array('jquery.auto-complete.min.js','jquery.buscar_proveedor.js'), false); ?>
<?php $javascript->link(array('jquery.adc_report.js'), false); ?>
<?php $javascript->link(array('home.js','home-dialogs.js'), false); ?>
<p/>
<div id="leftPanel">
    <div id="acc1">
    <?php
        function extractIcon($t){
            $i = null;
            if (strpos($t,'[') < strpos($t,']')){
                $i = substr($t, strpos($t,'[') + 1, strpos($t,']') - 1);
                $t = substr($t, strpos($t,']') + 1);
            }
            return $i;
        }
        function extractTitulo($t){
            $i = null;
            if (strpos($t,'[') < strpos($t,']')){
                $i = substr($t, strpos($t,'[') + 1, strpos($t,']') - 1);
                $t = substr($t, strpos($t,']') + 1);
            }
            return $t;
        }
        $lastdepth = 0;
        $depth = 1;
        $store = array();
        foreach ($acos as $aco){
            if (empty($aco['Aco']['orden'])) continue;

            $titleText = extractTitulo($aco['Aco']['titulo']);
            if (extractIcon($aco['Aco']['titulo']) == '')
                $iconText = '';
            else
                $iconText = empty($aco['Aco']['titulo'])||trim($aco['Aco']['titulo'])==''?
                        '':$html->image(extractIcon($aco['Aco']['titulo']), array('border'=>0));
            $orden = $aco['Aco']['orden'];

            $titleGroup = extractTitulo($aco['Aco']['grupo']);
            $grupos = explode('/',$titleGroup);
    //        $titleGroup = $grupos[count($grupos)-1];

            $depth = strlen($aco['Aco']['orden']) / 2;
            $diff = $lastdepth - $depth;
            if (empty($aco['Aco']['parent_id'])){
                if ($diff > 0) echo '</div>';
                echo '<h3>'.$html->link(sprintf('%s %s', $iconText, $titleText, $depth, $lastdepth, $diff),
                        '#', array('escape'=>false)).'</h3>';
                echo '<div>';
                $store = array();
            } else {
                // primero el grupo
                if (count($grupos) > 0 && trim($titleGroup) != ''){
                    $path = '';
                    $i = 0;
                    $done = 0;
                    $lastGroupDepth = $lastdepth;
                    $groupDepth = $i + 2;
                    foreach ($grupos as $grupo){
                        $iconGroup = empty($titleGroup)||trim($aco['Aco']['grupo'])==''||$groupDepth!=2 ?
                                '':$html->image(extractIcon($aco['Aco']['grupo']), array('border'=>0));
                        if ($i > 0) $path .= '/';
                        $groupDepth = $i + 2;
                        $groupDiff = $lastGroupDepth - $groupDepth;
                        $path .= $grupo;
                        if (!in_array($path, $store)){
                            if ($groupDepth == 2){
                                if ($groupDiff > 1) for ($i=0;$i<$groupDiff-1;$i++) echo '</li></ul>';
                                if ($groupDiff >= 0) echo '</ul>';
                                echo $html->link(sprintf('%s %s', $iconGroup, $grupo, $groupDepth, $lastGroupDepth, $groupDiff),
                                        '#', array('escape'=>false));
                                echo '<ul>';
                            } else if ($groupDepth > 2){
                                if ($groupDiff > 1) for ($i=0;$i<$groupDiff-1;$i++) echo '</li></ul>';
                                if ($groupDiff > 0) echo '</ul>';
                                echo '<li>';
                                echo $html->link(sprintf('%s %s', $iconGroup, $grupo, $groupDepth, $lastGroupDepth, $groupDiff),
                                        '#', array('escape'=>false));
                                echo '<ul>';
                            }
                            array_push($store, $path);
                            $done++;
                            $lastGroupDepth = $groupDepth;
                        }
                        $groupDepth++;
                        $i++;
                    }
                    if ($done > 0)
                        $lastdepth = $lastGroupDepth;
                }
                if (!empty($aco['Aco']['titulo'])) {
                    // luego el titulo
                    $depth = strlen($aco['Aco']['orden']) / 2;
                    $diff = $lastdepth - $depth;
                    if (!empty($aco['Aco']['url']) && trim($aco['Aco']['url'])!='') {
                        $link = $aco['Aco']['url'];
                    } else {
                        if (!empty($aco['Parent']['parent_id'])){
                            if ($aco['Parent']['alias'] == up($aco['Parent']['alias']))
                                $link = sprintf('/%s/index',$aco['Aco']['alias']);
                            else
                                $link = sprintf('/%s/%s',$aco['Parent']['alias'],$aco['Aco']['alias']);
                        } else $link = sprintf('/%s/index',$aco['Aco']['alias']);
                    }
                    if ($depth == 2){
                        if ($diff > 1) for ($i=0;$i<$diff;$i++) echo '</li></ul>';
                        if ($diff > 0) echo '</ul>';
                        echo $html->link(sprintf('%s %s', $iconText, $titleText, $depth, $lastdepth, $diff),
                                $link, array('escape'=>false));
                    } else {
                        if ($diff > 0) for ($i=0;$i<$diff;$i++) echo '</li></ul>';
                        echo '<li>'.$html->link(sprintf('%s %s', $iconText, $titleText, $depth, $lastdepth, $diff),
                                $link, array('escape'=>false)).'</li>';
                    }
                }
            }
            $lastdepth = $depth;
        }
        if ($lastdepth > 0) echo '</div>'
    ?>
    </div>
</div>
<div id="rightPanel">
    <div id="selectorcontent" class="ui-accordion-header ui-helper-reset ui-state-default ui-corner-all">
        <div id="h"></div>
        <div id="c" style="width: 200px;margin:0;">
            <?php echo $form->create(null, array('url'=>'/Home/index'),array('id'=>'homeForm'));?>
            <table id="selector" align="center">
                <tr>
                    <td width="100">Fecha de trabajo</td>
                    <td><div class="simpleDate"><?php echo $form->input('Empresa.fecha',array(
                        'type'=>'text',
                        'div'=>false,
                        'label'=>false,
                        'readonly'=>true,
                        'size'=>10,
                        'value'=>$session->read('Ejercicio.Actual.fecha')
                    ));?></div></td>
                </tr>
            </table>
            <?php echo $form->end();?>
        </div>
        <div id="f"></div>
    </div>
    <p/>
</div>
<div class="clear"></div>
<script type="text/javascript">
    $(function() {
        $('#EmpresaEmpresaId').val('<?php echo $usuario['Tercero']['empresa_id'];?>');
        $('#EmpresaCentroDistId').val('<?php echo $usuario['Tercero']['centro_dist_id'];?>');
        $('#EmpresaFecha').val('<?php echo $ejercicio['Actual']['fecha'];?>');
    });
</script>
<style type="text/css">
textarea{
    width:730px;
    margin:5px 0;
    padding:3px;
}
#leftPanel{
    width: 220px;
    height: auto;
    min-height: 500px;
    float: left;
    padding: 0;
    /*border:1px solid black;*/
}
#rightPanel{
    margin-left: 10px;
    margin-bottom: 10px;
    padding:0 10px 10px;
    width: 750px;
    height: auto;
    min-height: 500px;
    float: left;
    /*border: 1px solid #0B4B84;*/
}
.clear{
    margin: 2px;
    clear: both;
}
#selector{
    width: 680px;
    height: auto;
}
#EmpresaEmpresaId, #EmpresaCentroDistId {
    width: 160px;
}
#acc1 div a{
    clear: left;
    display: block;
    margin-left: -20px;
}
ul li{
    position: relative;
    clear:left;
    cursor:pointer;
    float:left;
    left: -35px;
    list-style-type: none;
    width: 150px;
}
.continous{
    display: inline;
    margin: 0;
    zoom: 1
}
</style>